package com.xh.fma.commodity.service.impl;


import com.xh.fma.commodity.dao.AttrAttrgroupRelationDao;
import com.xh.fma.commodity.dao.AttrgroupDao;
import com.xh.fma.commodity.entity.AttrAttrgroupRelationEntity;
import com.xh.fma.commodity.entity.AttrgroupEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xh.common.utils.PageUtils;
import com.xh.common.utils.Query;

import com.xh.fma.commodity.dao.AttrDao;
import com.xh.fma.commodity.entity.AttrEntity;
import com.xh.fma.commodity.service.AttrService;

import javax.annotation.Resource;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Resource
    AttrAttrgroupRelationDao relationDao;

    @Resource
    AttrgroupDao attrgroupDao;

    @Override
    public void saveAttrAndRelation(AttrEntity attr) {
        this.save(attr);

        if (attr.getAttrType() == 1 && attr.getAttrGroupId() != null){
            AttrAttrgroupRelationEntity relationEntity
                    = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrGroupId(attr.getAttrGroupId());
            relationEntity.setAttrId(attr.getAttrId());
            relationDao.insert(relationEntity);
        }
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageBySelect(Map<String, Object> params, Long categoryId,int attrType) {

        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_type",attrType);

        if (categoryId != 0){
            wrapper.eq("category_id",categoryId);
        }
        String key = (String)params.get("key");
        if (StringUtils.isNotEmpty(key)){
            wrapper.eq("attr_id",key).or().like("attr_name",key);
        }

        IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params),
                wrapper);

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByRelation(Map<String, Object> params, Long attrGroupId) {
        // 根据属性分组表查询到商品id
        AttrgroupEntity attrgroupEntity = attrgroupDao.selectById(attrGroupId);
        // 根据商品id查询到其对应的所有属性分组
        Long categoryId = attrgroupEntity.getCategoryId();
        List<AttrgroupEntity> attrgroupEntities =
                attrgroupDao.selectList(new QueryWrapper<AttrgroupEntity>().eq("category_id", categoryId));
        // 获取到所有的属性分组id
        List<Long> attrGroupIds = attrgroupEntities.stream().map(AttrgroupEntity::getId).collect(Collectors.toList());
        // 去关联表中  通过上面查到的所有属性分组id  获得已经关联的所有商品的属性id
        List<AttrAttrgroupRelationEntity> relationEntities =
                relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", attrGroupIds));
        List<Long> attrIds = relationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());

        // 从当前表  即属性表中进行查询并返回  先通过查询器设置查询条件
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        // 设置查询条件
        // 根据category_id查询
        wrapper.eq("category_id", categoryId).eq("attr_type",1);
        // 除去已经关联的属性
        if ( attrIds!= null && attrIds.size() > 0) {
            wrapper.notIn("attr_id ",attrIds);
        }
        // 如果携带查询条件
        String key = (String)params.get("key");
        if (StringUtils.isNotBlank(key)){
            wrapper.and(item -> item.eq("attr_id",key).or().like("attr_name",key));
        }

        IPage<AttrEntity> page =
                this.page(new Query<AttrEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }


}